package cn.iocoder.yudao.module.yyzx.controller.admin.outings;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.yyzx.controller.admin.outings.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.outings.OutingsDO;
import cn.iocoder.yudao.module.yyzx.service.outings.OutingsService;

@Tag(name = "管理后台 - 外出管理")
@RestController
@RequestMapping("/yyzx/outings")
@Validated
public class OutingsController {

    @Resource
    private OutingsService outingsService;

    @PostMapping("/create")
    @Operation(summary = "创建外出管理")
    @PreAuthorize("@ss.hasPermission('yyzx:outings:create')")
    public CommonResult<Integer> createOutings(@Valid @RequestBody OutingsSaveReqVO createReqVO) {
        return success(outingsService.createOutings(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新外出管理")
    @PreAuthorize("@ss.hasPermission('yyzx:outings:update')")
    public CommonResult<Boolean> updateOutings(@Valid @RequestBody OutingsSaveReqVO updateReqVO) {
        outingsService.updateOutings(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除外出管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yyzx:outings:delete')")
    public CommonResult<Boolean> deleteOutings(@RequestParam("id") Integer id) {
        outingsService.deleteOutings(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得外出管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yyzx:outings:query')")
    public CommonResult<OutingsRespVO> getOutings(@RequestParam("id") Integer id) {
        OutingsDO outings = outingsService.getOutings(id);
        return success(BeanUtils.toBean(outings, OutingsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得外出管理分页")
    @PreAuthorize("@ss.hasPermission('yyzx:outings:query')")
    public CommonResult<PageResult<OutingsRespVO>> getOutingsPage(@Valid OutingsPageReqVO pageReqVO) {
        PageResult<OutingsDO> pageResult = outingsService.getOutingsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OutingsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出外出管理 Excel")
    @PreAuthorize("@ss.hasPermission('yyzx:outings:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOutingsExcel(@Valid OutingsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OutingsDO> list = outingsService.getOutingsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "外出管理.xls", "数据", OutingsRespVO.class,
                        BeanUtils.toBean(list, OutingsRespVO.class));
    }

}